<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="../../layui/css/layui.css">
<script src="../../js/jquery-3.1.1.min.js" type="text/javascript" ></script>
<script src="js/ehy_common.js" type="text/javascript" ></script>
 
<script src="../../layui/layui.js"></script>
</head>
<body style="padding:20px 40px 0px 40px;">
	<form class="layui-form" id="mesFrom" method="post" lay-filter="from">
		<input type="hidden" name="modId" id="modId" />
		<div class="layui-form-item">
			<label class="layui-form-label">上級模块</label>
			<div class="layui-input-block">
				<input type="text" id="one" lay-filter="pidSelect" placeholder="不选为根模块" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">模块编码</label>
			<div class="layui-input-inline">
				<input type="text" name="modCode" id="modCode" required
					lay-verify="required|modCode" placeholder="模块编码" autocomplete="off"
					class="layui-input">
			</div>
			<div class="layui-form-mid layui-word-aux"></div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">URL地址</label>
			<div class="layui-input-block">
				<input type="text" name="url" id="url" lay-verify="url"
					placeholder="网址，若要设置为一级模块可以不填" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">模块名称</label>
			<div class="layui-input-inline">
				<input type="text" name="modName" id="modName" required
					lay-verify="required" placeholder="模块名称" autocomplete="off"
					class="layui-input">
			</div>
			<div class="layui-form-mid layui-word-aux"></div>
		</div>
		<div class="layui-form-item layui-form-text">
			<label class="layui-form-label">模块说明</label>
			<div class="layui-input-block">
				<textarea name="modDesc" id="modDesc" placeholder="请输入内容" class="layui-textarea"></textarea>
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<input type="button" class="layui-btn" lay-submit="" lay-filter="demo" value="提交">
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</div>
		<input type="hidden" name="oper" value="admin" />
	</form>
	
	<script type="text/javascript">
	
	 layui.config({
		base:"/drug/res/"
	}).extend({ //设定模块别名
		  
			  treeSelect: 'js/treeSelect' //相对于上述 base 目录的子目录
			}); 
	 
	//注意：parent 是 JS 自带的全局对象，可用于操作父页面
	var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
	var list;
	var modIds;
	$(function(){
		var modId = GetQueryString("modId");
		if(modId=='undefined'){
			list = '';
		}else{
			findById(modId);
		}
		var ehyModId = GetQueryString("ehyModId");
		showSelect(ehyModId, modId, list);
	});
	
	function showSelect(ehyModId, modId, list){
		
		layui.use(['treeSelect','layer','form'], function () {
			var treeSelect = layui.treeSelect,layer = layui.layer;
			treeSelect.render(
            {
                elem: "#one", 
                data: "../../drugModule/findAllByGroup.action",
                method: "post",
                key:{
                	id:'id'
                },
                click: function(node){
                	
                	modIds = node.id;
                	
                } 
            });
            
            if(modId!='undefined'){
            	modIds=ehyModId;
            	layer.msg("数据加载中.....", {
					  icon: 1,
					  time: 1 //1秒关闭（如果不配置，默认是3秒）
					}, function(){
						treeSelect.checkNode('pidSelect',ehyModId);
				});
            	
            }
            
        });
		layui.use('form', function() {
			var form = layui.form;
			form.render('select', 'from');
			//自定义验证规则
			form.verify({
				modCode : function(value){
					if(value.indexOf(" ")>-1){
						return "模块编码不能包含空格";
					}
				},
				url : function(value){
					if(value.indexOf(" ")>-1){
						return "模块地址不能包含空格";
					}
				},
				modName : function(value){
					if(value.indexOf(" ")>-1){
						return "模块名称不能包含空格";
					}
				}
			});
			
			//监听提交
			form.on('submit(demo)', function(data){
				  
				var data = $.param({"ehyModId":modIds}) + "&" +$("#mesFrom").serialize();
				var url = "../../drugModule/saveOrUpdate.action";
					
				$.post(url, data, function(msg){
					
					layer.msg(msg.msg, {
						  icon: 1,
						  time: 1000 //1秒关闭（如果不配置，默认是3秒）
						}, function(){
						  parent.location.reload();
					});
				});
			});
		});
	}
	
	
	function findById(id){
		var data = {"modId":id};
		var url = "../../drugModule/findById.action";
		if(id!=null || id!=""){
			$.ajax({
				type : "POST",
				url : url, 
				data : data, 
				async : false,
				success : function(obj){
					$("#modId").val(obj.modId);
					$("#modName").val(obj.modName);
					$("#modDesc").val(obj.modDesc);
					$("#modCode").val(obj.modCode);
					$("#url").val(obj.url);
					list = obj.children;
				}
			});
		}
	}
	
	/**
		得到URL栏的参数
	*/
	function GetQueryString(name)
	{
	     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	     var r = window.location.search.substr(1).match(reg);
	     if(r!=null)return  unescape(r[2]); return null;
	}
	
	</script>
</body>
</html>